<?php

/**
 * religion actions.
 *
 * @package    erp-dnict
 * @subpackage religion
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class religionActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
    $q = Doctrine_Query::create()
            ->from('Religion r')
            ->addWhere('r.is_active = ?', true);

    $this->religions = $q->execute();
    return sfView::SUCCESS;
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->religion_id = $request->getParameter('id', null);
    $this->form = new ReligionForm();

    if ($this->religion_id) {
      $religion = Doctrine_Core::getTable('Religion')->find($this->religion_id);
      $this->form = new ReligionForm($religion);
    }

    if ($request->isMethod('post')) {
      $form_data = $request->getParameter('religion');
      $this->form->bind($form_data);
      if ($this->form->isValid()) {
        if (!$request->getParameter('id'))
          $religion = $this->form->save();
        else { // update
          $religion = Doctrine_Core::getTable('Religion')->find($request->getParameter('id'));
          if ($religion) {
            $religion->name = $form_data['name'];
            $religion->description = $form_data['description'];
            $religion->is_active = $form_data['is_active'] == 'on' ? true : false;
            $religion->save();
          }
        }
        $this->redirect('@religion_index');
      }
    }
    return sfView::SUCCESS;
  }

  public function executeRemove(sfWebRequest $request)
  {
    $religion_id = $request->getParameter('id', null);
    if ($religion_id) {
      $religion = Doctrine_Core::getTable('Religion')->find($religion_id);
      if ($religion) {
        foreach ($religion->Profiles as $profile) {
          $profile->religion_id = 0;
          $profile->save();
        }
        $religion->is_active = false;
        $religion->save();
        $this->redirect('@religion_index');
      }
    }
  }
}
